<?php
namespace app\index\controller;

use com\wechat\TPQYWechat;
use think\Config;
use think\Controller;
use think\Input;

class Index extends Controller
{
    public function index() {
        $Wechat = new TPQYWechat(Config::get('zhongguancun'));
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $jsSign = $Wechat->getJsSign($url);
        dump($jsSign);

    }

    public function oauth(){
        $Wechat = new TPQYWechat(Config::get('zhongguancun'));
        $Wechat->valid();
    }

    /**
     * 添加用户到通讯录
     */
    public function register() {
        if (IS_POST) {
            $Wechat = new TPQYWechat(Config::get('zhongguancun'));
            $data = array(
                'userid' => Input::post('mobile'),
                'name' => Input::post('name'),
                'department' => '64', // 默认在未关注组中
                'mobile' => Input::post('mobile'),
                'gender' => Input::post('gender'),
                'extattr' => array('attrs'=>array(array('name'=>'所属单位', 'value'=>Input::post('unit'))))
            );

            $result = $Wechat->createUser($data);
            if($result) {
                if($result['errcode']== 0) {
                    return $this->success("添加成功");
                } else {
                    return $this->error($result['errmsg']);
                }
            } else {
                return $this->error($Wechat->errMsg);
            }
        } else {
            $Wechat = new TPQYWechat(Config::get('zhongguancun'));
            $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $jsSign = $Wechat->getJsSign($url);
            $this->assign("jsSign", $jsSign);

            return $this->fetch();
        }
    }


}
